﻿Imports System.ComponentModel
Imports Co9320.Sekatyaku

Public Class VideoViewModel
    Implements INotifyPropertyChanged
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    Protected Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me,
                                   New PropertyChangedEventArgs(propertyName))

    End Sub

    Private Model As Video

    Public Property Id As String
        Get
            Return Model.Id
        End Get
        Set(ByVal value As String)
            Model.Id = value
            OnPropertyChanged("Id")
        End Set
    End Property

    Public Property Title As String
        Get
            Return Model.Title
        End Get
        Set(ByVal value As String)
            Model.Title = value
            OnPropertyChanged("Title")
        End Set
    End Property

    Public Property Description As String
        Get
            Return Model.Description
        End Get
        Set(ByVal value As String)
            Model.Description = value
            OnPropertyChanged("Description")
        End Set
    End Property

    Public Property Length As TimeSpan
        Get
            Return Model.Length
        End Get
        Set(ByVal value As TimeSpan)
            Model.Length = value
            OnPropertyChanged("Length")
        End Set
    End Property

    Public Property FirstRetrieveDateTime As DateTime
        Get
            Return Model.FirstRetrieveDateTime
        End Get
        Set(ByVal value As DateTime)
            Model.FirstRetrieveDateTime = value
            OnPropertyChanged("FirstRetrieveDateTime")
        End Set
    End Property

    Public Property ThumbnailUri As Uri
        Get
            Return Model.ThumbnailUri
        End Get
        Set(ByVal value As Uri)
            Model.ThumbnailUri = value
            OnPropertyChanged("ThumbnailUri")
        End Set
    End Property

    Public Sub New(ByVal model As Video)
        Me.Model = model
    End Sub

    Public Function ToModel() As Video
        Return Model
    End Function

    Public Sub DownloadInfoAsync()
        Dim thread = New System.Threading.Thread(
                     AddressOf DownloadInfo)
        thread.Start()
    End Sub

    Private Sub DownloadInfo()
        Try
            Dim doc = XDocument.Load("http://ext.nicovideo.jp/api/getthumbinfo/" & Id)
            Title = doc.<nicovideo_thumb_response>.<thumb>.<title>.Value
            Description = doc.<nicovideo_thumb_response>.<thumb>.<description>.Value
            ThumbnailUri = New Uri(doc.<nicovideo_thumb_response>.<thumb>.<thumbnail_url>.Value)

            Dim l = doc.<nicovideo_thumb_response>.<thumb>.<length>.Value
            Dim i = l.IndexOf(":")
            Dim m = l.Substring(0, i)
            Dim s = l.Substring(i + 1)
            Length = New TimeSpan(0, Convert.ToInt32(m), Convert.ToInt32(s))

            FirstRetrieveDateTime =
                DateTime.Parse(doc.<nicovideo_thumb_response>.<thumb>.<first_retrieve>.Value)





        Catch ex As Exception

        End Try


    End Sub
End Class
